<?php

namespace app\apiz\controller\product;

use think\Controller;
use think\Cookie;

class Cart extends Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->TypeModel = model('Product.Type');
        $this->ProductModel = model('Product.Product');
        $this->UserModel = model('User.User');
        $this->CartModel = model('Cart');
    }
    public function add()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $proid = $this->request->param('proid', 0);
            $price = $this->request->param('price', 0);
            $nums = $this->request->param('nums', 0);

            $User = $this->UserModel->find($userid);

            if(!$User)
            {
                $this->error('用户不存在');
                exit;
            }

            $product = $this->ProductModel->find($proid);

            if(!$product)
            {
                $this->error('商品不存在');
                exit;
            }
            
            $where =[
                'userid' => $userid,
                'proid' => $proid
            ];
            // 判断是否已经添加过商品
            $cart = $this->CartModel->where($where)->find();
            if($cart)
            {
                $nums = $cart['nums']+$nums;
                
                $total = bcmul($nums, $price);

                $data = [
                    'id' => $cart['id'],
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => $nums,
                    'price' => $price,
                    'total' => $total,
                ];

                //更新数据库
                $result = $this->CartModel->validate('common/Cart')->isUpdate(true)->save($data);

                if($result === FALSE)
                {
                    $this->error($this->CartModel->getError());
                    exit;
                }else
                {
                    $this->success('添加购物车成功');
                    exit;
                }
            }else
            {
                $total = bcmul($nums, $price);
                
                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => $nums,
                    'price' => $price,
                    'total' => $total,
                ];
                $result = $this->CartModel->validate('common/Cart')->save($data);

                if($result === FALSE)
                {
                    $this->error($this->CartModel->getError());
                    exit;
                }else
                {
                    $this->success('添加购物车成功');
                    exit;
                }
            }
        }
    
                        
    }

    public function index()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $cartids = $this->request->param('cartids', '');

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }

            $where = [
                'userid' => $userid
            ];

            if(!empty($cartids))
            {
                $where['cart.id'] = ['in', $cartids];
            }

            //查询购物车记录
            $cartlist = $this->CartModel->with(['product'])->where($where)->select();

            $this->success('返回购物车记录成功', null, $cartlist);
            exit;
        }
    }

    public function edit()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $cartid = $this->request->param('cartid', 0);
            $nums = $this->request->param('nums', 1);

            $where = [
                'id' => $cartid,
                'userid' => $userid
            ];

            $cart = $this->CartModel->where($where)->find();
            if(!$cart)
            {
                $this->error('购物车记录不存在');
                exit;
            }

            $user = $this->UserModel->find($userid);

            if(!$user)
            {
                $this->error('用户不存在');
                exit;
            }
            $price = $cart['price'];
            //总价
            $total = bcmul($nums, $price);

            //组装数据
            $data = [
                'id' => $cartid,
                'nums' => $nums,
                'price' => $price,
                'total' => $total,
            ];

            //执行更新语句
            $result = $this->CartModel->isUpdate(true)->save($data);

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('更新购物车成功');
                exit;
            }
        }
    }
    //删除
    public function del()
    {
        if($this->request->isPost())
        {
            $userid = $this->request->param('userid', 0);
            $cartid = $this->request->param('cartid', 0);

            $where = [
                'id' => $cartid,
                'userid' => $userid
            ];

            $cart = $this->CartModel->where($where)->find();

            if(!$cart)
            {
                $this->error('购物车记录不存在');
                exit;
            }

            //删除记录
            $result = $this->CartModel->destroy($cartid);

            if($result === FALSE)
            {
                $this->error($this->CartModel->getError());
                exit;
            }else
            {
                $this->success('删除购物车成功');
                exit;
            }
        }
    }

  
}